package give.my.money.back;

import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class UserInformationListActivity extends ListActivity {

	private SimpleCursorAdapter myAdapter;
	private String[] currentUser_id;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.debtorslist);

		// Get the Contact ID passed as value from TabDebtorsList Activity.
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			currentUser_id = new String[] { extras.getString("contactID") };
		}

		DeudasSQLiteHelper databaseHelper = new DeudasSQLiteHelper(this, null,
				3);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();
		// Seach all debts to the current user.
		Cursor c = db.query("deudas", null, databaseHelper._ID + "=?",currentUser_id, null, null, null);
		        // db.query("", columns, selection, selectionArgs, groupBy, having, orderBy, limit)

		startManagingCursor(c); 

		// start mappings
		String[] columns = new String[] { databaseHelper.QUANTITY, databaseHelper.DESCRIPTION };
		int[] names = new int[] { R.id.quantity, R.id.description };

		SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(this,R.layout.header2, c, columns, names);
		setListAdapter(myAdapter);

		c.moveToFirst();// Go to first register to get the contact name.
		TextView txtcontactName = (TextView) findViewById(R.id.textView_headerUser);
		txtcontactName.setText(c.getString(c.getColumnIndex(databaseHelper.NAME)));

		Bitmap contactPic;
		contactPic = databaseHelper.loadContactPhoto(getContentResolver(),
				c.getLong(c.getColumnIndex(databaseHelper.RAW_CONTACT_ID)));
		
		ImageView iv = (ImageView)findViewById(R.id.imageView1);
		if (contactPic != null) {
			iv.setImageBitmap(contactPic);
		} else {// Set a default image if the contact has not one.
			iv.setImageResource(R.drawable.ic_launcher);
		}

	}

}
